package day4;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * ClassName：TestMAp
 *
 * @Author：小鱼
 * @Create：2025-08-14 9:21
 */
public class TestMAp {
    public static void main(String[] args) {
        //创建一个HashMap集合
        Map<Integer,String> map1 = new HashMap<>();

        //添加元素
        String s1 = map1.put(0, "hello");
        String s2 = map1.put(1, "hello");
        String s3 = map1.put(0, "hello0");//会覆盖之前的值

        System.out.println("s1; "+s1);
        System.out.println("s2; "+s2);
        System.out.println("s3; "+s3);

        System.out.println("长度："+map1.size());
        System.out.println("map: "+map1);

        //使用map.put时，先判断这里有没有值
        int key = 3;
        if(!map1.containsKey(key)){
            map1.put(key, "newValue");
        }
        System.out.println("长度："+map1.size());
        System.out.println("map: "+map1);


        //查询
        System.out.println(map1.get(0));
        System.out.println(map1.get(1));
        System.out.println(map1.get(2));
        System.out.println(map1.get(3));
        System.out.println(map1.get(4));

        //删除
        System.out.println(map1.remove(0));
        System.out.println(map1.remove(4));
        System.out.println("长度："+map1.size());
        System.out.println("map: "+map1);

//        map1.clear();
//        System.out.println("长度："+map1.size());
//        System.out.println("map: "+map1);


        //遍历方法一
        Set<Integer> set = map1.keySet();
        System.out.println(set);
        for (Integer i:set) {
            System.out.println(map1.get(i));
        }
        //遍历方法二
        Collection<String> coll =map1.values();
        for (String s: coll) {
            System.out.println(s);
        }


        map1.put(7, null);
        map1.put(8, null);
        map1.put(9, null);
        map1.put(null, "1");
        map1.put(null, "2");

        //遍历方法三
        Set<Map.Entry<Integer,String>> entries = map1.entrySet();
        System.out.println("entries:"+entries);
        for (Map.Entry<Integer, String> entry : entries) {
            entry.setValue("hi");
            System.out.print(entry.getKey()+"-");
            System.out.println(entry.getValue());
        }


    }
}
